//
//  DateConvertTool.m
//  Test
//
//  Created by zhaoshijie on 23/12/2017.
//  Copyright © 2017 zhaoshijie. All rights reserved.
//

#import "SJDateConvertTool.h"

@implementation SJDateConvertTool



/// 获取现在的时间戳
+ (double)getThePresentTimeStamp
{
    NSDate *date = [NSDate date];
    double timeStamp = [date timeIntervalSince1970];
    
    return timeStamp;
}



/// 获取现在的时间
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
+ (NSString *)getThePresentTimeWithDateDisplayType:(DateDisplayType)dateDisplayType
                                  customTypeString:(NSString *)customTypeString
{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setDateFormat: [self getDateDisplayTypeWithType:dateDisplayType customTypeString:customTypeString]];
    
    NSDate *date = [NSDate date];
    
    NSString *timeString = [dateformatter stringFromDate:date];
    
    return timeString;
}



/// 时间戳转换成字符串
/// @param timeStamp 时间戳
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
+ (NSString *)dateConvertTimeStampToStringWithTimeStamp:(double)timeStamp
                                        dateDisplayType:(DateDisplayType)dateDisplayType
                                       customTypeString:(NSString *)customTypeString
{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setDateFormat: [self getDateDisplayTypeWithType:dateDisplayType customTypeString:customTypeString]];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    
    NSString *timeString = [dateformatter stringFromDate:date];
    
    return timeString;
}




/// 字符串转换成时间戳
/// @param timeString 时间字符串
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
+ (double)dateConvertStringToTimeStampWithTimeString:(NSString *)timeString
                                   dateDisplayType:(DateDisplayType)dateDisplayType
                                  customTypeString:(NSString *)customTypeString
{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setDateFormat: [self getDateDisplayTypeWithType:dateDisplayType customTypeString:customTypeString]];
    
    NSDate *date = [dateformatter dateFromString:timeString];
    
    double timeStamp = [date timeIntervalSince1970];
    
    return timeStamp;
}




/// 字符串转换成字符串  （精确时间转换成模糊时间,，例如：yyyy-MM-dd HH:mm:ss -> HH:mm）
/// @param timeString 时间字符串
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
+ (NSString *)dateConvertStringToStringWithTimeString:(NSString *)timeString
                                      dateDisplayType:(DateDisplayType)dateDisplayType
                                     customTypeString:(NSString *)customTypeString
{
    //  时间戳
    double timeStap = [self dateConvertStringToTimeStampWithTimeString:timeString dateDisplayType:DateDisplayType_Default customTypeString:nil];
    
    //  时间字符串
    NSString *newTimeString = [self dateConvertTimeStampToStringWithTimeStamp:timeStap dateDisplayType:dateDisplayType customTypeString:customTypeString];
    
    return newTimeString;
}



/// 债券列表时间
/// @param timeString 时间
/// @param dateDisplayType 类型
/// @param customTypeString 自定义
+ (NSString *)bondListTimeConversionWithTimeString:(NSString *)timeString
                                   dateDisplayType:(DateDisplayType)dateDisplayType
                                  customTypeString:(NSString *)customTypeString
{
    
    //  时间戳
    double timeStamp = [SJDateConvertTool dateConvertStringToTimeStampWithTimeString:timeString dateDisplayType:DateDisplayType_Default customTypeString:customTypeString];

    double nowTimeStamp = [SJDateConvertTool getThePresentTimeStamp];
    
    
    NSString *time = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Date customTypeString:nil];
    
    double timeStapDefault = [SJDateConvertTool dateConvertStringToTimeStampWithTimeString:time dateDisplayType:DateDisplayType_Date customTypeString:nil];
    
    
    NSString *nowTime = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:nowTimeStamp dateDisplayType:DateDisplayType_Date customTypeString:nil];
    
    double nowTimeStapDefault = [SJDateConvertTool dateConvertStringToTimeStampWithTimeString:nowTime dateDisplayType:DateDisplayType_Date customTypeString:nil];

    double distanceTime = timeStapDefault - nowTimeStapDefault;

    NSString * distanceStr;

    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];

    NSDateFormatter * df = [[NSDateFormatter alloc]init];

    [df setDateFormat:@"yyyy-MM-dd"];

    NSString * nowDay = [df stringFromDate:[NSDate date]];

    NSString * lastDay = [df stringFromDate:beDate];
    
    
    if([nowDay isEqualToString:lastDay])
    {
        NSString *timestring = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Custom customTypeString:@"HH:mm:ss"];
        
        distanceStr = [NSString stringWithFormat:@"今天 %@",timestring];
    }
    else if(distanceTime <= 24*60*60 && distanceTime>0)
    {
        NSString *timestring = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Custom customTypeString:@"HH:mm:ss"];
        
        distanceStr = [NSString stringWithFormat:@"明日 %@",timestring];

    }
    else
    {
        NSString *timestring = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Custom customTypeString:@"MM.dd HH:mm:ss"];
        
        distanceStr = [NSString stringWithFormat:@"%@",timestring];
    }

    

    return distanceStr;

}




/// 社区时间转换
/// @param timeString 时间
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
+ (NSString *)communityTimeConversionWithTimeString:(NSString *)timeString
                                    dateDisplayType:(DateDisplayType)dateDisplayType
                                   customTypeString:(NSString *)customTypeString
{
    
    //  时间戳
    double timeStamp = [SJDateConvertTool dateConvertStringToTimeStampWithTimeString:timeString dateDisplayType:DateDisplayType_Default customTypeString:customTypeString];

    double nowTimeStamp = [SJDateConvertTool getThePresentTimeStamp];
    
    
    NSString *time = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Date customTypeString:nil];
    
    double timeStapDefault = [SJDateConvertTool dateConvertStringToTimeStampWithTimeString:time dateDisplayType:DateDisplayType_Date customTypeString:nil];
    
    
    NSString *nowTime = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:nowTimeStamp dateDisplayType:DateDisplayType_Date customTypeString:nil];
    
    double nowTimeStapDefault = [SJDateConvertTool dateConvertStringToTimeStampWithTimeString:nowTime dateDisplayType:DateDisplayType_Date customTypeString:nil];

    double distanceTime = nowTimeStapDefault - timeStapDefault;

    NSString * distanceStr;

    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];

    NSDateFormatter * df = [[NSDateFormatter alloc]init];

    [df setDateFormat:@"yyyy-MM-dd"];

    NSString * nowDay = [df stringFromDate:[NSDate date]];

    NSString * lastDay = [df stringFromDate:beDate];
    
    
    if([nowDay isEqualToString:lastDay])
    {
        NSString *timestring = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Custom customTypeString:@"HH:mm:ss"];
        
        distanceStr = [NSString stringWithFormat:@"今天 %@",timestring];
    }
    else if(distanceTime <= 24*60*60)
    {
        NSString *timestring = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Custom customTypeString:@"HH:mm:ss"];
        
        distanceStr = [NSString stringWithFormat:@"昨天 %@",timestring];

    }
    else
    {
        NSString *timestring = [SJDateConvertTool dateConvertTimeStampToStringWithTimeStamp:timeStamp dateDisplayType:DateDisplayType_Custom customTypeString:@"yyyy-MM-dd"];
        
        distanceStr = [NSString stringWithFormat:@"%@",timestring];
    }

    

    return distanceStr;
}



/// 时间戳转换成字符串
/// @param timeStamp 时间戳
+ (NSString *)dateConvertTimeStampToLatelyStringWithTimeStamp:(double)timeStamp
{
    NSInteger seconds = @(timeStamp).integerValue;
    
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@时%@分%@秒",str_hour,str_minute,str_second];
    
    return format_time;
}





/// 获取多少天之前之后的日期时间
/// @param timeString 时间字符串
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
/// @param dayNum 天数
/// @param directionType 之前之后方向
+ (NSString *)gainPreviousDateWithBeginTimeString:(NSString *)timeString
                                  dateDisplayType:(DateDisplayType)dateDisplayType
                                 customTypeString:(NSString *)customTypeString
                                           dayNum:(NSInteger)dayNum
                                    directionType:(PreviousDateDirectionType)directionType
{
    //  获取时间戳
    double timeStamp = [self dateConvertStringToTimeStampWithTimeString:timeString dateDisplayType:dateDisplayType customTypeString:customTypeString];
    
    //  时间差
    double timeDiff = dayNum*24*60*60;
    
    //  新的时间戳
    double newTimeStamp ;
    if(directionType == PreviousDateDirectionType_Before)
    {
        //  之前
        newTimeStamp = timeStamp - timeDiff;
    }
    else
    {
        //  之后
        newTimeStamp = timeStamp + timeDiff;
    }
    
    //  获取时间字符串
    NSString *newTimeString = [self dateConvertTimeStampToStringWithTimeStamp:newTimeStamp dateDisplayType:dateDisplayType customTypeString:customTypeString];
    
    return newTimeString;
        
}



/// 获取当前周几
+ (NSString*)getCurrentWeekDay
{
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    
    return [self getWeekDayFordate:interval];
    
}




/// 获取对应时间戳周几
/// @param data 时间戳
+ (NSString *)getWeekDayFordate:(NSTimeInterval)data
{
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate];
    
    NSString *weekStr = [weekday objectAtIndex:components.weekday];
    
    return weekStr;
    
}





/// 获取转换类型
/// @param dateDisplayType 转换类型
/// @param customTypeString 自定义类型，如果不需要的话设置nil
+ (NSString *)getDateDisplayTypeWithType:(DateDisplayType)dateDisplayType
                        customTypeString:(NSString *)customTypeString
{
    NSString *timeTypeString = nil;
    
    switch (dateDisplayType)
    {
        case DateDisplayType_Default:
        {
            timeTypeString = @"yyyy-MM-dd HH:mm:ss";
            break;
        }
        case DateDisplayType_Date:
        {
            timeTypeString = @"yyyy-MM-dd";
            break;
        }
        case DateDisplayType_Time:
        {
            timeTypeString = @"HH:mm";
            break;
        }
        case DateDisplayType_Custom:
        {
            //  自定义类型不正确的话设置默认类型
            timeTypeString = customTypeString ? customTypeString : @"yyyy-MM-dd HH:mm:ss";
            break;
        }
        default:
        {
            //  找不到的话设置默认类型
            timeTypeString = @"yyyy-MM-dd HH:mm:ss";
            break;
        }
    }
    return timeTypeString;
}






@end
